package com.yaodian100.ec.order.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.ec.order.entity.OrderPrize;
import com.yaodian100.ec.order.service.OrderPrizeService;

public class OrderPrizeServiceImpl extends DomainServiceImpl<OrderPrize> implements OrderPrizeService {

	@Override
	public List<Object> getOrderPrize(Map<String, Object> orderPrizeMap) throws CoreException {
		StringBuffer sb = new StringBuffer();
		List<String> attrs = new ArrayList<String>();
		sb.append("select p.prize_id,t.tuangou_id,p.product_id,p.prod_name,p.order_date,t.start_date,t.end_date,p.login_id,p.prize_mobile,p.prize_place,t.price,p.channel,p.member_id,p.order_st from ec_shelf_tuangou t,ec_order_prize p ");
		sb.append("where t.tuangou_id=p.tuangou_id ");

		if (null != orderPrizeMap.get("prizeId")) {
			sb.append("and p.prize_id= ? ");
			attrs.add(orderPrizeMap.get("prizeId").toString());
		}
		if (null != orderPrizeMap.get("tuangouId")) {
			sb.append("and t.tuangou_id= ? ");
			attrs.add(orderPrizeMap.get("tuangouId").toString());
		}
		if (null != orderPrizeMap.get("prizeMobile")) {
			sb.append("and p.prize_mobile= ? ");
			attrs.add(orderPrizeMap.get("prizeMobile").toString());
		}
		if (null != orderPrizeMap.get("prodName")) {
			sb.append("and p.prod_name like ? ");
			attrs.add("%" + orderPrizeMap.get("prodName").toString() + "%");
		}
		if (null != orderPrizeMap.get("loginId")) {
			sb.append("and p.login_id = ? ");
			attrs.add(orderPrizeMap.get("loginId").toString());
		}
		if (null != orderPrizeMap.get("prizedate")) {
			sb.append("and to_date(?,'yyyy-MM-dd hh24:mi:ss') between t.start_date and t.end_date ");
			attrs.add(orderPrizeMap.get("prizedate").toString());
		}
		List list = getDao().getSQLQueryByList(sb.toString(), attrs, 0, -1);
		return list;
	}

	@Override
	public Number getOrderPrizeSize(Map<String, Object> orderPrizeMap) throws CoreException {
		StringBuffer sb = new StringBuffer();
		List<String> attrs = new ArrayList<String>();
		sb.append("select p.prize_id,t.tuangou_id,p.product_id,p.prod_name,p.order_date,t.start_date,t.end_date,p.login_id,p.prize_mobile,p.prize_place,t.price,p.channel,p.order_st from ec_shelf_tuangou t,ec_order_prize p ");
		sb.append("where t.tuangou_id=p.tuangou_id ");

		if (null != orderPrizeMap.get("prizeId")) {
			sb.append("and p.prize_id= ? ");
			attrs.add(orderPrizeMap.get("prizeId").toString());
		}
		if (null != orderPrizeMap.get("tuangouId")) {
			sb.append("and t.tuangou_id= ? ");
			attrs.add(orderPrizeMap.get("tuangouId").toString());
		}
		if (null != orderPrizeMap.get("prizeMobile")) {
			sb.append("and p.prize_mobile= ? ");
			attrs.add(orderPrizeMap.get("prizeMobile").toString());
		}
		if (null != orderPrizeMap.get("prodName")) {
			sb.append("and p.prod_name like ? ");
			attrs.add("%" + orderPrizeMap.get("prodName").toString() + "%");
		}
		if (null != orderPrizeMap.get("loginId")) {
			sb.append("and p.login_id = ? ");
			attrs.add(orderPrizeMap.get("loginId").toString());
		}
		if (null != orderPrizeMap.get("prizedate")) {
			sb.append("and to_date(?,'yyyy-MM-dd') between t.start_date and t.end_date ");
			attrs.add(orderPrizeMap.get("prizedate").toString());
		}
		List list = getDao().getSQLQueryByList(sb.toString(), attrs, 0, -1);
		return list.size();
	}

	@Override
	public List<Object> getOrderExportPrize(Map<String, Object> orderPrizeMap) throws CoreException {
		StringBuffer sb = new StringBuffer();
		List<String> attrs = new ArrayList<String>();

		sb.append("select t.tuangou_id,p.promote_name,p.prod_name,t.start_date,t.end_date,t.lowest_order_qty,t.price,t.order_qty,p.order_st ");
		sb.append("from ec_shelf_tuangou t,ec_order_prize p ");
		sb.append("where t.tuangou_id=p.tuangou_id ");

		if (null != orderPrizeMap.get("productId")) {
			sb.append("and p.product_id = ? ");
			attrs.add(orderPrizeMap.get("productId").toString());
		}
		if (null != orderPrizeMap.get("tuangouId")) {
			sb.append("and t.tuangou_id= ? ");
			attrs.add(orderPrizeMap.get("tuangouId").toString());
		}
		if (null != orderPrizeMap.get("prizedate")) {
			sb.append("and to_date(?,'yyyy-MM-dd hh24:mi:ss') between t.start_date and t.end_date ");
			attrs.add(orderPrizeMap.get("prizedate").toString());
		}
		if (null != orderPrizeMap.get("prodName")) {
			sb.append("and p.prod_name like ? ");
			attrs.add("%" + orderPrizeMap.get("prodName").toString() + "%");
		}
		sb.append("group by t.tuangou_id,p.promote_name,p.prod_name,t.start_date,t.end_date,t.lowest_order_qty,t.price,t.order_qty,p.order_st ");
		List list = getDao().getSQLQueryByList(sb.toString(), attrs, 0, -1);
		return list;
	}

	@Override
	public Number getOrderPrizeExportSize(Map<String, Object> orderPrizeMap) throws CoreException {
		StringBuffer sb = new StringBuffer();
		List<String> attrs = new ArrayList<String>();

		sb.append("select t.tuangou_id,p.promote_name,p.prod_name,t.start_date,t.end_date,t.lowest_order_qty,t.price,t.order_qty,p.order_st ");
		sb.append("from ec_shelf_tuangou t,ec_order_prize p ");
		sb.append("where t.tuangou_id=p.tuangou_id ");

		if (null != orderPrizeMap.get("productId")) {
			sb.append("and p.product_id = ? ");
			attrs.add(orderPrizeMap.get("productId").toString());
		}
		if (null != orderPrizeMap.get("tuangouId")) {
			sb.append("and t.tuangou_id= ? ");
			attrs.add(orderPrizeMap.get("tuangouId").toString());
		}
		if (null != orderPrizeMap.get("prizedate")) {
			sb.append("and to_date(?,'yyyy-MM-dd hh24:mi:ss') between t.start_date and t.end_date ");
			attrs.add(orderPrizeMap.get("prizedate").toString());
		}
		if (null != orderPrizeMap.get("prodName")) {
			sb.append("and p.prod_name like ? ");
			attrs.add("%" + orderPrizeMap.get("prodName").toString() + "%");
		}
		sb.append("group by t.tuangou_id,p.promote_name,p.prod_name,t.start_date,t.end_date,t.lowest_order_qty,t.price,t.order_qty,p.order_st ");
		List list = getDao().getSQLQueryByList(sb.toString(), attrs, 0, -1);
		return list.size();
	}
}
